package com.song.service.email;

import com.song.entity.EmailEntity;
import com.song.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.nio.file.FileSystem;

@Service("emailService")
public class EmailServiceImpl implements EmailService {
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private MailProperties mailProperties;

    /**
     * 发送简单的邮件
     * @return 发送是否成功
     */
    @Override
    public Result sendSimpleEmail(EmailEntity emailEntity) {
        Result result = new Result();
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(mailProperties.getUsername());
            message.setTo(emailEntity.getToUsername());
            message.setSubject(emailEntity.getSubject());
            message.setText(emailEntity.getContent());
            mailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("邮件发送失败");
            result.setCode(-1);
            return result;
        }
        result.setMsg("邮件发送成功");
        result.setCode(0);
        return result;
    }

    /**
     * 发送带有资源邮件
     * @param emailEntity
     * @return
     */
    @Override
    public Result sendResource(EmailEntity emailEntity) {
        Result result = new Result();
        try {
            MimeMessage  message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(mailProperties.getUsername());
            helper.setTo(emailEntity.getToUsername());
            // 发送主题
            helper.setSubject(emailEntity.getSubject());
            // html文本格式
            helper.setText(emailEntity.getContent());
            mailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setMsg("带有附件的邮件发送失败！");
            return result;
        }
        result.setMsg("邮件发送成功");
        result.setCode(0);
        return result;
    }

    /**
     * 发送带有附件的邮件
     * @param emailEntity
     * @return
     */
    @Override
    public Result sendAttachment(EmailEntity emailEntity) {
        Result result = new Result();
        try {
            MimeMessage  message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(mailProperties.getUsername());
            helper.setTo(emailEntity.getToUsername());
            helper.setSubject(emailEntity.getSubject());
            helper.setText(emailEntity.getContent());
            FileSystemResource file = new FileSystemResource(new File(emailEntity.getFilePath()));
            helper.addAttachment(emailEntity.getFilePath(), file);
            mailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setMsg("带有附件的邮件发送失败！");
            return result;
        }
        result.setMsg("邮件发送成功");
        result.setCode(0);
        return result;
    }
}
